import Vue from 'vue'
import './public-path'
import 'normalize.css/normalize.css' // A modern alternative to CSS resets
// import Avue from '@smallwei/avue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// import '@smallwei/avue/lib/index.css'
import locale from 'element-ui/lib/locale/lang/en' // lang i18n

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import '@/icons' // icon
import '@/permission' // permission control

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online ! ! !
 */
if (process.env.NODE_ENV === 'production') {
  const { mockXHR } = require('../mock')
  mockXHR()
}
// 处理label文字过长
const addMouted = {}
addMouted.mounted = function() {
  document.getElementsByClassName('el-form-item__label').forEach((item) => {
    if (item.clientHeight >= 45) {
      item.style.lineHeight = '13px'
    }
  })
}
ElementUI.Input.mixins.push(addMouted)
// set ElementUI lang to EN
Vue.use(ElementUI, { locale })
// Vue.use(Avue)
// 如果想要中文版 element-ui，按如下方式声明
// Vue.use(ElementUI)
Vue.prototype.$myAppfor = 'KaiXinKuaiLe'
window.$myAppfor = 'KaiXinKuaiLe'
Vue.config.productionTip = false
import { registerMicroApps, initGlobalState, start, addGlobalUncaughtErrorHandler } from 'qiankun'

// new Vue({
//   el: '#app',
//   router,
//   store,
//   render: h => h(App)
// })

// let router = null
let instance = null
function render(props = {}) {
  const { container } = props
  // router = new VueRouter({
  //   base: window.__POWERED_BY_QIANKUN__ ? '/app-vue/' : '/',
  //   mode: 'history',
  //   routes,
  // })
  instance = new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount(container ? container.querySelector('#app') : '#app')
}
// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  render()
} else {
  // eslint-disable-next-line camelcase,no-undef
  __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__
}
const actions = initGlobalState()

// 设置监听
actions.onGlobalStateChange((value, prev) => console.log('[onGlobalStateChange - master]:', value, prev))
Vue.prototype.$actions = actions
// 引发变化
// setGlobalState({
//   ignore: 'master',
//   user: {
//     name: 'master'
//   }
// })

export async function bootstrap() {
  console.log('[vue] vue app bootstraped')
}
export async function mount(props) {
  console.log('[vue] props from main framework', props)
  render(props)
}
export async function update(props) {
  console.log('[vue] props from main update', props)
  render(props)
}
export async function unmount() {
  instance.$destroy()
  instance.$el.innerHTML = ''
  instance = null
  router = null
}
// 注册子应用
registerMicroApps([
  {
    name: 'modu2', // 子应用名称
    entry: process.env.NODE_ENV === 'production' ? '/modu2/' : '//localhost:8081/modu2/ ', // 子应用入口
    container: '#modul-content', // 子应用所在容器
    activeRule: '/modu2', // 子应用触发规则（路径）
    props: {
      data: { store, router }
    },
    loader(loading) {
      console.log('loader bloolean', loading)
    }
  }
])

addGlobalUncaughtErrorHandler(event => console.log('qian kun err ====', event))

// 启动默认应用
// setDefaultMountApp('/modu2')

// 开启服务
start()

